<?php
/**
 * 测试
 */
class PaijuTableHandler extends ScriptBase
{
    const PAIZU_TABLE = 'log_paiju';//log_paiju
    public function logic()
    {
        $mysql = $this->mysql('logs');
        set_time_limit(0);
        while (true)
        {
            $date = date("Y-m-d");
            $dataTime =  strtotime($date);
            $timestamp = $dataTime - 3600*32;
            $timestampStart = $dataTime - 3600*24;
            $timestampEnd = $dataTime - 3600*48;
            $table = self::PAIZU_TABLE.'_'.date("Ymd", $timestamp);
            $hasTable = $mysql->tableExists($table);  //判断是否有该表
            $createFlag = $hasTable ? true : false;
            if( empty( $createFlag ) ){
                //创建表
                $createSql = "CREATE TABLE {$table} LIKE log_paiju_toku";
                $createFlag = $mysql->query($createSql);
            }
            if( $createFlag ){
                //$sql = "select id from log_paiju where paizu_time between '{$timestampEnd}' and '{$timestampStart}' order by paizu_time desc limit 1";
                $sql1 = "INSERT INTO {$table}(paizu_id,seq,creator,room_id,server_id,uids,`time`,detail,`status`,juShu,paizu_time,isMatch,reward,clubID,clubName,clubConfigID,hexWanFa) SELECT paizu_id,seq, creator,room_id,server_id,uids,`time`,detail,`status`,juShu,paizu_time,isMatch,reward,clubID,clubName,clubConfigID,hexWanFa FROM log_paiju where paizu_time between '{$timestampEnd}' and '{$timestampStart}' order by paizu_time desc limit 100";
                $sql2 = "delete from log_paiju where paizu_time between '{$timestampEnd}' and '{$timestampStart}' order by paizu_time desc limit 100";
                $rs = $mysql->findRow('log_paiju',"paizu_time between '{$timestampEnd}' and '{$timestampStart}'",['fields'=>'id','order'=>'paizu_time desc']);
                if( empty($rs) ){
                    break;
                    exit(0);
                }
                // echo "{$sql}\n";
                if($mysql->query($sql1)){
                     !$mysql->query($sql2);
                }
            }
            sleep(5);
        }
    }
}